package com.learn.shop.service.impl;

import com.learn.shop.pojo.entriy.SUser;
import com.learn.shop.mapper.SUserMapper;
import com.learn.shop.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户登录表(SUser)表服务实现类
 *
 * @author zhengdechang
 * @since 2021-12-20 16:31:30
 */
@Service
public class SUserServiceImpl implements SUserService {
    @Autowired
    private SUserMapper sUserMapper;


    @Override
    public SUser queryByName(String username) {
        return sUserMapper.queryByName(username);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SUser queryById(String userId) {
        return this.sUserMapper.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sUser       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SUser> queryByPage(SUser sUser, PageRequest pageRequest) {
        long total = this.sUserMapper.count(sUser);
        return new PageImpl<>(this.sUserMapper.queryAllByLimit(sUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sUser 实例对象
     * @return 实例对象
     */
    @Override
    public SUser insert(SUser sUser) {
        this.sUserMapper.insert(sUser);
        return sUser;
    }

    /**
     * 修改数据
     *
     * @param sUser 实例对象
     * @return 实例对象
     */
    @Override
    public SUser update(SUser sUser) {
        this.sUserMapper.update(sUser);
        return this.queryById(sUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String userId) {
        return this.sUserMapper.deleteById(userId) > 0;
    }
}
